/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AnnotationBuilder.java
 *
 * Created on Jun 14, 2010, 1:33:03 PM
 */

package UserInterface;

import java.io.File;

/**
 *
 * @author leng
 */
public class AnnotationBuilder extends javax.swing.JFrame {

    protected int selecetedtext_start;
    protected int selecetedtext_end;
    protected String selectedtext;
    protected UserInterface.GUI eHOST_GUI;
    

    /** Creates new form AnnotationBuilder */
    public AnnotationBuilder(UserInterface.GUI eHOST_GUI) {
        this.eHOST_GUI = eHOST_GUI;
        initComponents();
        prepareAnnotationClassnames();
        jTextField_creationDate.setText(commons.OS.getCurrentDate());
    }


    /**load all annotation classnames into the combolist*/
    private void prepareAnnotationClassnames(){
        // empty the combobox before to add annotation classnames
        jComboBox_AnnotationClassnames.removeAllItems();

        // get all annotation classnames
        ResultEditor.AnnotationClasses.Depot depot =
                new ResultEditor.AnnotationClasses.Depot();
        String[] annotationClassnames = depot.getAnnotationClasssnamesString();

        // add classnames into the combobox
        int size = annotationClassnames.length;
        for(int i=0;i<size;i++){
            jComboBox_AnnotationClassnames.addItem(annotationClassnames[i]);
        }

    }

    public void setValueofSelectedText(final String selectedtext,
            final int selecetedtext_start,
            final int selecetedtext_end
            ){
        jTextField_spantext.setText(selectedtext);
        this.selecetedtext_end = selecetedtext_end;
        this.selecetedtext_start = selecetedtext_start;
        this.selectedtext = selectedtext;
        String location = "["+selecetedtext_start+", "+selecetedtext_end+"]";
        //jTextField_location.setText(location);
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton_save = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField_spantext = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_creationDate = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jComboBox_AnnotationClassnames = new javax.swing.JComboBox();
        jComboBox_AnnotationClassnames1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Manually add an annotation");
        setBackground(new java.awt.Color(236, 233, 216));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(236, 233, 216));

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton_save.setText("Add");
        jButton_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_saveActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(290, Short.MAX_VALUE)
                .add(jButton_save)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton_save))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel2.setBackground(new java.awt.Color(236, 233, 216));

        jLabel1.setText("Annotation Text:");

        jLabel3.setText("Creation Date");

        jLabel4.setText("Annotator:");

        jComboBox_AnnotationClassnames.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Markable:");

        jLabel6.setText("Relationships:");

        jScrollPane1.setViewportView(jList1);

        jLabel7.setText("Comments:");

        jScrollPane2.setViewportView(jList2);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel3)
                        .add(18, 18, 18)
                        .add(jTextField_creationDate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField_spantext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 285, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel6)
                            .add(jLabel7)
                            .add(jLabel5))
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                .add(19, 19, 19)
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jComboBox_AnnotationClassnames1, 0, 285, Short.MAX_VALUE)
                                    .add(jComboBox_AnnotationClassnames, 0, 285, Short.MAX_VALUE)))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))))))
                .add(39, 39, 39))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField_spantext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField_creationDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jComboBox_AnnotationClassnames, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jComboBox_AnnotationClassnames1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel6)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_saveActionPerformed
        // TODO add your handling code here:
        saveNewAnnotation_intoMemory();
    }//GEN-LAST:event_jButton_saveActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    private int assignMeAUniqueIndex(){
         return ResultEditor.Annotations.AnnotationIndex.newAnnotationIndex();
    }

    private void saveNewAnnotation_intoMemory(){
        String testsourceFilename = ResultEditor.WorkSpace.WorkSet.getCurrentFile().getName().trim();

        ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
        depot.articleInsurance( testsourceFilename );
        String annotator_name = ResultEditor.Annotator.Manager.getAnnotatorName_OutputOnly();
        String annotator_id = ResultEditor.Annotator.Manager.getAnnotatorID_outputOnly();
        depot.addANewAnnotation( testsourceFilename, this.selectedtext,
                selecetedtext_start, selecetedtext_end,
                // creation date
                jTextField_creationDate.getText(),
                // annotation classname
                jComboBox_AnnotationClassnames.getSelectedItem().toString(),
                annotator_name,
                annotator_id,
                null,
                null,
                assignMeAUniqueIndex()
                );
        this.setVisible(false);
        eHOST_GUI.setEnabled(true);
        eHOST_GUI.showAnnotationCategoriesInTreeView_refresh();
        eHOST_GUI.repaintHighlighter();
        eHOST_GUI.removeSelection();
        eHOST_GUI.showValidPositionIndicators();
        this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton_save;
    private javax.swing.JComboBox jComboBox_AnnotationClassnames;
    private javax.swing.JComboBox jComboBox_AnnotationClassnames1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField_creationDate;
    private javax.swing.JTextField jTextField_spantext;
    // End of variables declaration//GEN-END:variables

}
